<template>
    <div id="CollaborativeDepartmentDetail">
      <!--v-if="rowData.ZTYPE==1"-->
      <van-field  name="checkboxGroup" label="协同部门">
        <template #input>
          <van-checkbox-group  @change="checkboxChange" v-model="checkboxGroup" direction="horizontal">
            <van-checkbox
              style="margin-bottom: 10px;"
              shape="square"
              v-for="(item,index) in  checkData"
              :key="index"
              :name="item.DEPT_NAME"
            >
              {{item.DEPT_NAME}}
            </van-checkbox>
          </van-checkbox-group>
        </template>
      </van-field>
      <van-field
        readonly
        clickable
        name="picker"
        :value="fromData.SPJL | inUseName(columns)"
        label="审批结论"
        placeholder="点击审批结论"
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-picker
          value-key="DICNAME"
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
      <van-field name="uploader" label="审批文件">
        <template #input>
          <van-uploader
            v-model="fileList"
            :after-read="afterRead"
            :before-delete="delectFile"
          />
        </template>
      </van-field>
      <van-field
        v-model="fromData.SPYJ"
        label="审批意见"
        rows="3"
        autosize
        type="textarea"
        placeholder="请填写审批意见"
      />
      <div style="background: #ffffff;padding: 10px;">
        <p style="text-align: right;">{{deptName}}{{fromData.SPRY}}  &nbsp;   {{getDate(true)}}</p>
      </div>
      <div style="position: absolute;bottom: 1%;left: 50%;transform: translateX(-50%);">
        <van-button @click="submitData" style="width: 90vw;" size="small" type="info">提交</van-button>
      </div>
    </div>
</template>

<script>
  import { FileServerProxy, dataTransFile,dataTransFilePreview} from '../../../utils/api'
  export default {
    filters: {
      inUseName: function(msg, data) {
        let name = "";
        data.forEach((item) => {
          if (item.DICID == msg) {
            name = item.DICNAME;
          }
        });
        return name;
      },
    },
    name: "CollaborativeDepartmentDetail",
    data(){
      return{
        deptName:'',
        fileList:[],
        columns:[
          {
            DICNAME:'拟同意',
            DICID:'9'
          },
          {
            DICNAME:'不同意',
            DICID:'10'
          }
        ],
        showPicker:false,
        rowData:"",
        checkboxGroup: [],
        fromData:{
          TO_ROW_GUID:'',
          XTBM:'',
          BMID:'',
          SPJL:'',
          SPYJ:'',
          SPRY:JSON.parse(window.localStorage.getItem('userName')).loginName,
          SPSJ:'',
          OPTYPE:'1',
          ROW_GUID:'',
          eca_RowState:'added',
        },
        checkData:[],
        uploader:[]
      }
    },
    methods:{
      onConfirm(data){
        this.showPicker = false
        this.fromData.SPJL  = data.DICID
      },
      getData: async function (){
        let params = {
          accessCode: '688bd3883572440a859dfd8ba9194e7b',
          cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
          params: {
            dataguid: '5799fa46-1b45-4495-b1ec-7202b5f817c8',
            searchObject: JSON.stringify([]),
            token: window.localStorage.getItem('token')
          }
        }
        const res = await FileServerProxy(params)
        let data = JSON.parse(res.data.data).Data
        console.log(data)
        this.checkData = data
      },
      afterRead(file) {
        if (Array.isArray(file)){
          file.forEach((item,index)=>{
            this.getFileServerUpload(item.file);
          })
        }else{
          this.getFileServerUpload(file.file);
        }
      },
      delectFile: async function (file) {
        let fileguid = "";
        if (file.ROW_GUID) {
          fileguid = file.ROW_GUID;
        } else {
          fileguid = this.fileGuid;
        }
        if (fileguid) {
          let params = {
            accessCode: "ce9b691634de4ebca68dc5bb638d07f4",
            cityCode: window.localStorage.getItem("citycode")
              ? window.localStorage.getItem("citycode")
              : "441300000000",
            params: {
              fileGuid: fileguid,
              token: window.localStorage.getItem("token"),
            },
          };
          const res = await FileServerProxy(params);
          let data = JSON.parse(res.data.data).Success;
          if (data) {
            this.$toast.success("删除成功!");
          }
        }
      },
      getFileServerUpload: async function (file) {
        let data = {
          toRowGuid: this.rowData.ROW_GUID,
          groupGuid: "5bc87529-3ee1-49b0-bde2-8204b4717976",
          token: window.localStorage.getItem("token"),
        };
        let formData = new FormData();
        formData.append("file", file);
        formData.append("accessCode", "7a3fc061c866434b8475d3775c9ddcea");
        formData.append("cityCode", window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000');
        formData.append("token", window.localStorage.getItem("token"));
        formData.append("paramStr", JSON.stringify(data));
        const res = await dataTransFile(formData);
        let data1 = JSON.parse(res.data.data).FileList;
        this.fileGuid = data1[0].FILEGUID;
        if (data1[0].FILEGUID) {
          this.$toast.success("上传成功!");
        }
        // this.getFile();
      },
      getFile: async function () {
        let params = {
          accessCode: "1fdcbce42c6d4748a020feba618d54b8",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "",
          params: {
            groupGuid: "5bc87529-3ee1-49b0-bde2-8204b4717976",
            toRowGuid: this.rowData.ROW_GUID,
            token: window.localStorage.getItem("token"),
          },
        };
        const res = await FileServerProxy(params);
        let data = JSON.parse(res.data.data).FilesInfo;
        if (data) {
          data.forEach((element) => {
            // this.ImgData.forEach((items) => {
              if (element.GROUPGUID == "5bc87529-3ee1-49b0-bde2-8204b4717976") {
                this.getFileUrl(element);
              }
            // });
          });
        }
      },
      //获取附件文件流
      getFileUrl: async function (element, item) {
        let formData = new FormData();
        formData.append("accessCode", "98bcf528966e48ce896a8b3fd02d45d5");
        formData.append(
          "cityCode",
          window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000"
        );
        formData.append(
          "paramStr",
          JSON.stringify({
            fileGuid: element.ROW_GUID,
          })
        );
        let response = await dataTransFilePreview(formData);
        if (response) {
          // item.fileList = []
          var img = new Blob([new Int8Array(response.data, 0)], {
            type: response.headers["content-type"],
          }); //type类型可自定义
          element["isImage"] = true;
          element["url"] = window.URL.createObjectURL(img);
          this.fileList.push(element);
          // console.log(item.fileList)
        }
        console.log(response)

      },
      getDeptData:async function (){
        let paerssendh = {
          procName: "get_deptName_by_userId",
          dtParams: JSON.stringify([{ I_USERID: window.localStorage.getItem('ISZHUCE')}])
        };
        let paramsendMEssageh = {
          accessCode: "69db6bece3a7480592e2ec753d1e335e",
          cityCode: window.localStorage.getItem("citycode")
            ? window.localStorage.getItem("citycode")
            : "441300000000",
          params: paerssendh
        };
        const reqwmsgh = await FileServerProxy(paramsendMEssageh);
        const JSONSendh = JSON.parse(reqwmsgh.data.data);
        console.log(JSONSendh)
        this.deptName = JSONSendh.Data[0]._DEPTNAME
      },
      submitData:async function (){
        console.log(this.fromData)
        let params = {
          accessCode: '0fb3f6123d1b4fe6851c99532c463f6f',
          cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '',
          params: {
            dataGuid: this.rowData.ZTYPE==1? 'a30c10be-3225-4e49-bd5b-531795ecb940':'a117332d-c2f9-4fb7-bef9-8b77264c256a',
            strParams: JSON.stringify([this.fromData]),
          },
        }
        const res = await FileServerProxy(params)
        let data = JSON.parse(res.data.data).Success
        console.log(data)
        if (data) {
          this.$toast.success("提交成功！");
        }else{
          this.$toast.fail('提交失败！')
        }
      },
      checkboxChange(data){
        console.log(data)
        this.fromData.XTBM = data.join(',')
        let arr = []
        this.checkData.forEach((item,index)=>{
          data.forEach((item1,index1)=>{
            // debugger
             if (item.DEPT_NAME==item1) {
               arr.push(item.DEPTID)
             }
          })
        })
        console.log(arr)
        this.fromData.BMID = arr.join(',')
        console.log(this.fromData)
      },
    },
    mounted(){
      this.fromData.ROW_GUID = this.newGuid()
      this.fromData.SPSJ = this.getDate(true)
      this.getData()
      this.getDeptData()
      if (this.$route.query.data) {
        this.rowData = JSON.parse(this.$route.query.data)
        this.fromData.TO_ROW_GUID = this.rowData.ROW_GUID
      }
    }
  };
</script>

<style scoped lang="scss">
 #CollaborativeDepartmentDetail{
   height: 100vh;
   overflow: hidden;
   background: #f7f8fa;
   padding: 10px;
 }
</style>
